package com.dushiguang.findhoney.View.Banner;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dushiguang.findhoney.R;

import java.util.List;

/**
 * Created by ex-dushiguang201 on 2015-11-30.
 */
public class ViewPagerOfBannerAdapter extends FragmentStatePagerAdapter {
    public static final int  LOOPS_COUNT = 1000;

    private static FragmentActivity activity;
    private List<String> mPhotos;
    private int[]mLocalPic;
    private ImageHandle mImageHandle;
    private int mItemResId;
    private ViewPager viewPager;

    private boolean isTrueItem = false;
    private View[] views = new View[3];
    private static boolean picIsFromNet = false;


    public ViewPagerOfBannerAdapter(FragmentActivity activity, List<String> photos, int[] LocalPic,ImageHandle handle,
                                    int itemResId, ViewPager viewPager) {
        super(activity.getSupportFragmentManager());

        this.activity = activity;
        this.mPhotos = photos;
        this.mImageHandle = handle;
        this.mItemResId = itemResId;
        this.viewPager = viewPager;
        this.mLocalPic = LocalPic;//使用app中的图片

        picIsFromNet = (mLocalPic.length == 0 ? true :false);
    }

    public void setLoader(ImageHandle handle) {
        this.mImageHandle = handle;
    }

    @Override
    public Fragment getItem(int position) {
        if (position <= 3 && !isTrueItem) {
            return new Fragment();
        } else {
            isTrueItem = true;
            int fragmentPos = position % views.length;
            if (Math.abs(viewPager.getCurrentItem() - position) != 1) {
                views = new View[views.length];
            }

            if (views[fragmentPos] == null) {
                views[fragmentPos] = View.inflate(activity, mItemResId, null);
            }

            if (picIsFromNet) {
                position = position % mPhotos.size();
                return PhotoFragment.newInstance(mPhotos.get(position), mImageHandle, views[fragmentPos], position);
            } else {
                //使用App中的图片
                position = position % mLocalPic.length;
                return PhotoFragment.newIntInstance(mLocalPic[position], mImageHandle, views[fragmentPos], position);
            }
        }

    }

    @Override
    public int getCount() {
        if (picIsFromNet && mPhotos != null && mPhotos.size() > 0) {
            return mPhotos.size() * LOOPS_COUNT;
        } else if (mLocalPic != null && mLocalPic.length > 0){
            return mLocalPic.length * LOOPS_COUNT;
        } else {
            return 1;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        ViewGroup viewGroup = (ViewGroup) ((Fragment) object).getView();
        if (viewGroup != null) {
//            viewGroup.removeAllViews();

        }
    }

    public static class PhotoFragment extends Fragment {
        private ImageHandle mImageHandle;
        private View        view;

        public static PhotoFragment newInstance(String photo, ImageHandle handle, View view, int position) {
            PhotoFragment fragment = new PhotoFragment();
            fragment.setView(view);
            Bundle args = new Bundle();
            args.putString("photo", photo);
            // args.putInt("itemResId", itemResId);
            args.putInt("position", position);
            fragment.setOnPhotoLoader(handle);
            fragment.setArguments(args);
            return fragment;
        }

        public static PhotoFragment newIntInstance(int photoId, ImageHandle handle, View view, int position) {
            PhotoFragment fragment = new PhotoFragment();
            fragment.setView(view);
            Bundle args = new Bundle();
            args.putInt("photo", photoId);
            args.putInt("position", position);
            fragment.setOnPhotoLoader(handle);
            fragment.setArguments(args);
            return fragment;
        }

        public void setOnPhotoLoader(ImageHandle handle) {
            this.mImageHandle = handle;
        }

        public void setView(View view) {
            this.view = view;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            if (view != null && view.getParent() != null)
                ((ViewGroup) view.getParent()).removeAllViews();
            return view;
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(this.view, savedInstanceState);
            if (picIsFromNet && mImageHandle != null) {
                ImageView imageView;
                imageView = (ImageView) this.view.findViewById(R.id.ui_pic_iv);
                mImageHandle.loadImage(imageView,
                        getArguments().getString("photo"), getArguments().getInt("position"));
            } else  {
                ImageView imageView;
                imageView = (ImageView) this.view.findViewById(R.id.ui_pic_iv);
                mImageHandle.loadLocalImage(imageView,
                        getArguments().getInt("photo"), getArguments().getInt("position"));
            }

        }
    }
}
